$(function(){
	getData();
});

function add(){
	$("#add").append("<div class='col-md-12'><div class='col-md-1 item'><i class='icon-trash delete' onclick='save(this)' data-id='7'></i></div><div class='col-md-5 content' ><input type='text' class='color addcontent' placeholder='请输入反馈类别' ></div></div>");
}
function getData(){
	var load_alert=loadAlert('请稍后...');
	$('#data').html('');
	$.ajax({
		type: "post",
		url: "Admin/CourseTypeConfig/getConfig",
		async: true,
		dataType:'json',
		success: function(data) {
			closeAlert(load_alert);
			if(data.status){
				var t=JSON.parse(data.v);
				$.each(t,function(k,v){
					$("#data").append("<div class='col-md-12'><div class='col-md-1 item'><i class='icon-trash delete' onclick='del(this)' data-id='"+v['id']+"'></i></div><div class='col-md-5 content' ><input class='config'value='"+v['option']+"' ></div></div>");
				});
			}
			else{
				window.parent.topMessagePrompt('error','获取数据失败');
			}
		}
	});
}
function del(obj){
	var	msg = "是否确认删除该类型?";	
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {			
			var load_alert=loadAlert('请稍后...');
			$.ajax({
				type: "post",
				url: "Admin/CourseTypeConfig/delConfig",
				async: true,
				dataType:'json',
				data:{
					id:$(obj).attr('data-id')
				},
				success: function(data) {
					if(data.status){
						window.parent.topMessagePrompt('success',data.msg,function(){
							$(obj).parents('.eval_question_option_line').remove();
						});				
						setTimeout("location.reload()",1000);
					}
					else{
						window.parent.topMessagePrompt('error','删除失败');
					}
				}
			});
		}
	});	
}

function save(obj){
	var name= new Array();
	$(".config").each(function(k, v) {
		name[k]=v.value;
	});
	$.ajax({
		url: "Admin/CourseTypeConfig/saveConfig",
		type: 'POST',
		dataType: 'json',
		data: {name: name},
		success:function(data){
			if(data.status){
				var t=JSON.parse(data.v);
				$("#add").html('');
				window.parent.topMessagePrompt('success','保存成功');
				getData();			
			}else{
				window.parent.topMessagePrompt('error',data.msg);
			}
			
			
		}
	})
	
}